package com.jingyong.infosite.service;

import com.jingyong.infosite.dto.request.PlatformSearchRequest;
import com.jingyong.infosite.dto.response.PlatDetailResponse;
import com.jingyong.infosite.dto.response.PlatformHotResponse;
import com.jingyong.infosite.dto.response.PlatformSearchResponse;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author Daniel Wang
 * @date 2018/1/24
 */
@Service
public class PlatformService {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final String LOAN_PLANT_DETAIL_DATABASE_NAME = "platform_detail";
    @Autowired
    private MongoTemplate mongoTemplate;

    public PlatformHotResponse queryPlatformHot() {
        MongoCollection<Document> col = mongoTemplate.getCollection("platform_detail");
        int maxCnt = 5;

        PlatformHotResponse platformHotResponse = new PlatformHotResponse();
        PlatformHotResponse.PlatformHotAssort pbAssort = new PlatformHotResponse.PlatformHotAssort();
        PlatformHotResponse.PlatformHotAssort riAssort = new PlatformHotResponse.PlatformHotAssort();
        PlatformHotResponse.PlatformHotAssort itAssort = new PlatformHotResponse.PlatformHotAssort();
        PlatformHotResponse.PlatformHotAssort odAssort = new PlatformHotResponse.PlatformHotAssort();

        PlatformHotResponse.PlatformHotUpDown pbAssortUp = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown pbAssortDown = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown riAssortUp = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown riAssortDown = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown itAssortUp = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown itAssortDown = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown odAssortUp = new PlatformHotResponse.PlatformHotUpDown();
        PlatformHotResponse.PlatformHotUpDown odAssortDown = new PlatformHotResponse.PlatformHotUpDown();

        List<PlatformHotResponse.PlatformHotInfo> items = new LinkedList<>();

        Pattern upPattern = Pattern.compile("^.*国资.*$", Pattern.CASE_INSENSITIVE);
        Pattern downPattern = Pattern.compile("^.*上市.*$", Pattern.CASE_INSENSITIVE);
        BasicDBObject upFilter = new BasicDBObject();
        upFilter.append("platform_detail.platform_info.platform_background", upPattern);
        BasicDBObject downFilter = new BasicDBObject();
        downFilter.append("platform_detail.platform_info.platform_background", downPattern);
        //检索出背景平台
        FindIterable<Document> platformBackgroundDocsUp
                = col.find(upFilter).sort(new BasicDBObject("create_time", -1)).limit(maxCnt);
        FindIterable<Document> platformBackgroundDocsDown
                = col.find(downFilter).sort(new BasicDBObject("create_time", -1)).limit(maxCnt);

        int i = 0;
        for (Document d : platformBackgroundDocsUp) {
            if (i >= maxCnt) {
                break;
            }
            Map<String, Object> map = (Map<String, Object>) d.get("platform_detail");
            PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
            item.setId(String.valueOf(map.get("_id")));
            item.setName(String.valueOf(map.get("name")));
            items.add(item);
            i++;
        }
        pbAssortUp.setLen(i);
        pbAssortUp.setItems(items);

        i = 0;
        items.clear();
        for (Document d : platformBackgroundDocsDown) {
            if (i >= maxCnt) {
                break;
            }
            Map<String, Object> map = (Map<String, Object>) d.get("platform_detail");
            PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
            item.setId(String.valueOf(map.get("_id")));
            item.setName(String.valueOf(map.get("name")));
            items.add(item);
            i++;
        }
        pbAssortDown.setLen(i);
        pbAssortDown.setItems(items);

        List<PlatformHotResponse.PlatformHotInfo> itemsRiUp = new LinkedList<>();
        List<PlatformHotResponse.PlatformHotInfo> itemsRiDown = new LinkedList<>();
        List<PlatformHotResponse.PlatformHotInfo> itemsItUp = new LinkedList<>();
        List<PlatformHotResponse.PlatformHotInfo> itemsItDown = new LinkedList<>();
        List<PlatformHotResponse.PlatformHotInfo> itemsOdUp = new LinkedList<>();
        List<PlatformHotResponse.PlatformHotInfo> itemsOdDown = new LinkedList<>();
        int riUp = 0;
        int riDown = 0;
        int itUp = 0;
        int itDown = 0;
        int odUp = 0;
        int odDown = 0;
        FindIterable<Document> documents = col.find(new BasicDBObject()).sort(new BasicDBObject("create_time", -1));
        for (Document d : documents) {
            if (riUp >= maxCnt && riDown >= maxCnt && itUp >= maxCnt && itDown >= maxCnt && odUp >= maxCnt && odDown >= maxCnt) {
                break;
            }
            if (d.containsKey("platform_detail")) {
                Map<String, Object> map = (Map<String, Object>) d.get("platform_detail");
                if (map.containsKey("platform_info")) {
                    Map<String, Object> map2 = (Map<String, Object>) map.get("platform_info");
                    if (riUp < maxCnt || riDown < maxCnt) {
                        if (map2.containsKey("refer_income") && !String.valueOf(map2.get("refer_income")).contains("-")) {
                            int num = Integer.parseInt(String.valueOf(map2.get("refer_income")).replaceAll("%", "").split("\\.")[0]);
                            if (num < 10 && riDown < maxCnt) {
                                PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
                                item.setId(String.valueOf(map.get("_id")));
                                item.setName(String.valueOf(map.get("name")));
                                itemsRiDown.add(item);
                                riDown++;
                            }
                            if (num >= 10 && riUp < maxCnt) {
                                PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
                                item.setId(String.valueOf(map.get("_id")));
                                item.setName(String.valueOf(map.get("name")));
                                itemsRiUp.add(item);
                                riUp++;
                            }
                        }
                    }
                    if (itUp < maxCnt || itDown < maxCnt) {
                        if (map2.containsKey("invest_term") && !String.valueOf(map2.get("invest_term")).contains("-")) {
                            int num = 0;
                            if (String.valueOf(map2.get("invest_term")).contains("年")) {
                                num = 12;
                            } else if (String.valueOf(map2.get("invest_term")).contains("月")) {
                                num = Integer.parseInt(String.valueOf(map2.get("invest_term")).replaceAll("月", "").split("\\.")[0]);
                            }

                            if (num < 3 && itDown < maxCnt) {
                                PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
                                item.setId(String.valueOf(map.get("_id")));
                                item.setName(String.valueOf(map.get("name")));
                                itemsItDown.add(item);
                                itDown++;
                            }
                            if (num >= 3 && itUp < maxCnt) {
                                PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
                                item.setId(String.valueOf(map.get("_id")));
                                item.setName(String.valueOf(map.get("name")));
                                itemsItUp.add(item);
                                itUp++;
                            }
                        }
                    }
                    if (odUp < maxCnt || odDown < maxCnt) {
                        if (map2.containsKey("online_date") && String.valueOf(map2.get("online_date")).length() > 4) {
                            int num = Integer.parseInt(String.valueOf(map2.get("online_date")).substring(0, 4));
                            int nowYear = Calendar.getInstance().get(Calendar.YEAR);
                            if (nowYear - num < 3 && odDown < maxCnt) {
                                PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
                                item.setId(String.valueOf(map.get("_id")));
                                item.setName(String.valueOf(map.get("name")));
                                itemsOdDown.add(item);
                                odDown++;
                            }
                            if (nowYear - num >= 3 && odUp < maxCnt) {
                                PlatformHotResponse.PlatformHotInfo item = new PlatformHotResponse.PlatformHotInfo();
                                item.setId(String.valueOf(map.get("_id")));
                                item.setName(String.valueOf(map.get("name")));
                                itemsOdUp.add(item);
                                odUp++;
                            }
                        }
                    }
                }
            }
        }

        riAssortUp.setLen(riUp);
        riAssortUp.setItems(itemsRiUp);
        riAssortDown.setLen(riDown);
        riAssortDown.setItems(itemsRiDown);
        itAssortUp.setLen(itUp);
        itAssortUp.setItems(itemsItUp);
        itAssortDown.setLen(itDown);
        itAssortDown.setItems(itemsItDown);
        odAssortUp.setLen(odUp);
        odAssortUp.setItems(itemsOdUp);
        odAssortDown.setLen(odDown);
        odAssortDown.setItems(itemsOdDown);

        pbAssort.setUp(pbAssortUp);
        pbAssort.setDown(pbAssortDown);
        riAssort.setUp(riAssortUp);
        riAssort.setDown(riAssortDown);
        itAssort.setUp(itAssortUp);
        itAssort.setDown(itAssortDown);
        odAssort.setUp(odAssortUp);
        odAssort.setDown(odAssortDown);

        platformHotResponse.setPlatform_background(pbAssort);
        platformHotResponse.setRefer_income(riAssort);
        platformHotResponse.setInvest_term(itAssort);
        platformHotResponse.setOnline_date(odAssort);

        return platformHotResponse;
    }

    public PlatformSearchResponse query(PlatformSearchRequest req) {
        MongoCollection<Document> col = mongoTemplate.getCollection("platform_detail");
        BasicDBObject filter = new BasicDBObject();
        FindIterable<Document> documents = col.find(filter).sort(new BasicDBObject("create_time", -1));
        PlatformSearchResponse platformSearchResponse = new PlatformSearchResponse();
        List<PlatformSearchResponse.PlatformSearchResponseItem> itemList = new LinkedList<>();
        int i = 0;
        int len = req.getLen() > 0 ? req.getLen() : 20;
        int total = 0;
        int page = req.getPage() > 0 ? req.getPage() : 1;
        int start = len * (page - 1);
        int end = start + len;
        for (Document document : documents) {
            if (document.containsKey("platform_detail")) {
                // 这里开始遍历列表 通过条件筛选对应的平台
                Map<String, Object> map = (Map<String, Object>) document.get("platform_detail");
                boolean flg = false;//0不限制  1符合 -1不符合
                PlatformSearchResponse.PlatformSearchResponseItem item = new PlatformSearchResponse.PlatformSearchResponseItem();
                // 平台背景
                String pbs = req.getPlatform_background();
                if (!pbs.contains("0")) {
                    // 1：国资控股 2：国资参股 3：上市控股 4：上市参股 5：风投系 6：银行系 7：民营系
                    boolean b = false;
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        if (mapSub.containsKey("platform_background")
                                && mapSub.get("platform_background") != null
                                && !String.valueOf(mapSub.get("platform_background")).contains("-")) {
                            String platform_background = String.valueOf(mapSub.get("platform_background"));
                            if (pbs.contains("1")) {
                                if (platform_background.contains("国资控股")) {
                                    b = true;
                                }
                            }
                            if (pbs.contains("2")) {
                                if (platform_background.contains("国资参股")) {
                                    b = true;
                                }
                            }
                            if (pbs.contains("3")) {
                                if (platform_background.contains("上市控股")) {
                                    b = true;
                                }
                            }
                            if (pbs.contains("4")) {
                                if (platform_background.contains("上市参股")) {
                                    b = true;
                                }
                            }
                            if (pbs.contains("5")) {
                                if (platform_background.contains("风投系")) {
                                    b = true;
                                }
                            }
                            if (pbs.contains("6")) {
                                if (platform_background.contains("银行系")) {
                                    b = true;
                                }
                            }
                            if (pbs.contains("7")) {
                                if (platform_background.contains("民营系")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 运营状态
                String rss = req.getRegistered_state();
                if (!rss.contains("0")) {
                    // 0：不限 1：运营中 2：停业或转型 3：问题平台
                    boolean b = false;
                    if (map.containsKey("IndustryCommerceInformation")
                            && map.get("IndustryCommerceInformation") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("IndustryCommerceInformation");
                        if (mapSub.containsKey("registered_state") && mapSub.get("registered_state") != null
                                && !String.valueOf(mapSub.get("registered_state")).contains("-")) {
                            String registered_state = String.valueOf(mapSub.get("registered_state"));
                            if (rss.contains("1")) {
                                if (registered_state.contains("存续") || registered_state.contains("在营")
                                        || registered_state.contains("开业") || registered_state.contains("在册")) {
                                    b = true;
                                }
                            }
                            if (rss.contains("2")) {
                                if (registered_state.contains("停业") || registered_state.contains("转型")) {
                                    b = true;
                                }
                            }
                            if (rss.contains("3")) {
                                if (registered_state.contains("问题")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 债权转让
                String dts = req.getDebt_transfer();
                if (!dts.contains("0")) {
                    // 0：不限 1：随时转让 2：1月后可转让 3：3月后可转让 4：1年后可转让 5：不可转让
                    boolean b = false;
                    if (map.containsKey("platform_service") && map.get("platform_service") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_service");
                        if (mapSub.containsKey("debt_transfer") && mapSub.get("debt_transfer") != null
                                && !String.valueOf(mapSub.get("debt_transfer")).contains("-")) {
                            String debt_transfer = String.valueOf(mapSub.get("debt_transfer"));
                            if (dts.contains("1")) {
                                if (debt_transfer.contains("随时")) {
                                    b = true;
                                }
                            }
                            if (dts.contains("2")) {
                                if (debt_transfer.contains("1个月")) {
                                    b = true;
                                }
                            }
                            if (dts.contains("3")) {
                                if (debt_transfer.contains("3个月")) {
                                    b = true;
                                }
                            }
                            if (dts.contains("4")) {
                                if (debt_transfer.contains("1年")) {
                                    b = true;
                                }
                            }
                            if (dts.contains("5")) {
                                if (debt_transfer.contains("不可")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 保障模式
                String bgs = req.getBid_guarantee();
                if (!bgs.contains("0")) {
                    // 0：不限 1：风险准备金 2：小贷公司 3：融资性担保公司 4：非融资性担保公司 5：平台垫付 6：其他
                    boolean b = false;
                    if (map.containsKey("platform_service") && map.get("platform_service") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_service");
                        if (mapSub.containsKey("guarantee_mode") && mapSub.get("guarantee_mode") != null
                                && !String.valueOf(mapSub.get("guarantee_mode")).contains("-")) {
                            String guarantee_mode = String.valueOf(mapSub.get("guarantee_mode"));
                            if (bgs.contains("1")) {
                                if (guarantee_mode.contains("风险准备金")) {
                                    b = true;
                                }
                            }
                            if (bgs.contains("2")) {
                                if (guarantee_mode.contains("小贷公司")) {
                                    b = true;
                                }
                            }
                            if (bgs.contains("3")) {
                                if (guarantee_mode.contains("融资性担保公司")) {
                                    b = true;
                                }
                            }
                            if (bgs.contains("4")) {
                                if (guarantee_mode.contains("非融资性担保公司")) {
                                    b = true;
                                }
                            }
                            if (bgs.contains("5")) {
                                if (guarantee_mode.contains("平台垫付")) {
                                    b = true;
                                }
                            }
                            if (bgs.contains("6")) {
                                if (guarantee_mode.contains("-")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 自动投标
                String abs = req.getAuto_bid();
                if (!abs.contains("0")) {
                    // 0：不限 1：支持 2：不支持
                    boolean b = false;
                    if (map.containsKey("platform_service") && map.get("platform_service") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_service");
                        if (mapSub.containsKey("auto_bid") && mapSub.get("auto_bid") != null
                                && !String.valueOf(mapSub.get("auto_bid")).contains("-")) {
                            String auto_bid = String.valueOf(mapSub.get("auto_bid"));
                            if (abs.contains("1")) {
                                if (auto_bid.contains("支持")) {
                                    b = true;
                                }
                            }
                            if (abs.contains("2")) {
                                if (auto_bid.contains("不支持")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 参考利益
                String ris = req.getRefer_income();
                if (!ris.contains("0")) {
                    // 0：不限 1：<=8% 2：8%~10% 3：10%~12% 4：12%~16% 5：>=16%
                    boolean b = false;
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        if (mapSub.containsKey("refer_income")
                                && mapSub.get("refer_income") != null
                                && !String.valueOf(mapSub.get("refer_income")).contains("-")) {
                            String refer_income = String.valueOf(mapSub.get("refer_income")).replaceAll("%", "");
                            // 7.26%
                            int d = Integer.parseInt(refer_income.split("\\.")[0]);
                            if (ris.contains("1")) {
                                if (d <= 8) {
                                    b = true;
                                }
                            }
                            if (ris.contains("2")) {
                                if (d >= 8 && d <= 10) {
                                    b = true;
                                }
                            }
                            if (ris.contains("3")) {
                                if (d >= 10 && d <= 12) {
                                    b = true;
                                }
                            }
                            if (ris.contains("4")) {
                                if (d >= 12 && d <= 16) {
                                    b = true;
                                }
                            }
                            if (ris.contains("5")) {
                                if (d >= 16) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 投资期限
                String its = req.getInvest_term();
                if (!its.contains("0")) {
                    // 0：不限 1：天标 2：1月标 3：2月标 4：3月标 5：4-6月标 6：6-12月标 7：12月以上标
                    boolean b = false;
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        if (mapSub.containsKey("invest_term") && mapSub.get("invest_term") != null
                                && !String.valueOf(mapSub.get("invest_term")).contains("-")) {
                            String invest_term = String.valueOf(mapSub.get("invest_term"));
                            if (!invest_term.contains("-")) {
                                // 7.26月
                                if (its.contains("1")) {
                                    if (invest_term.contains("天")) {
                                        b = true;
                                    }
                                }
                                if (invest_term.contains("月")) {
                                    int d = Integer.parseInt(invest_term.split("月")[0].split("\\.")[0]);
                                    if (its.contains("2")) {
                                        if (d == 1) {
                                            b = true;
                                        }
                                    }
                                    if (its.contains("3")) {
                                        if (d == 2) {
                                            b = true;
                                        }
                                    }
                                    if (its.contains("4")) {
                                        if (d == 3) {
                                            b = true;
                                        }
                                    }
                                    if (its.contains("5")) {
                                        if (d >= 4 && d <= 6) {
                                            b = true;
                                        }
                                    }
                                    if (its.contains("6")) {
                                        if (d >= 6 && d <= 12) {
                                            b = true;
                                        }
                                    }
                                    if (its.contains("7")) {
                                        if (d >= 12) {
                                            b = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 待还余额
                String rbs = req.getRemain_balance();
                if (!rbs.contains("0")) {
                    // 0：不限 1：<=2亿 2：2-10亿 3：10-50亿 4：>=50亿
                    boolean b = false;
                    if (map.containsKey("data") && map.get("data") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("data");
                        if (mapSub.containsKey("remain_balance") && mapSub.get("remain_balance") != null
                                && !String.valueOf(mapSub.get("remain_balance")).contains("-")) {
                            String remain_balance = String.valueOf(mapSub.get("remain_balance"));
                            // 29,026.11
                            int d = Integer.parseInt(remain_balance.split("\\.")[0].replaceAll(",", ""));
                            if (rbs.contains("1")) {
                                if (d <= 20000) {
                                    b = true;
                                }
                            }
                            if (rbs.contains("2")) {
                                if (d >= 20000 && d <= 100000) {
                                    b = true;
                                }
                            }
                            if (rbs.contains("3")) {
                                if (d >= 100000 && d <= 500000) {
                                    b = true;
                                }
                            }
                            if (rbs.contains("4")) {
                                if (d >= 500000) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 近30日成交
                String dvs = req.getD30_vulome();
                if (!dvs.contains("0")) {
                    // 0：不限 1：<=1亿 2：1-5亿 3：5-10亿 4：>=10亿
                    boolean b = false;
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        if (mapSub.containsKey("d30_vulome") && mapSub.get("d30_vulome") != null
                                && !String.valueOf(mapSub.get("d30_vulome")).contains("-")) {
                            String d30_vulome = String.valueOf(mapSub.get("d30_vulome"));
                            if (!d30_vulome.contains("-")) {
                                // 29,026.11
                                int d = Integer.parseInt(d30_vulome.split("\\.")[0]);
                                if (dvs.contains("1")) {
                                    if (d <= 10000) {
                                        b = true;
                                    }
                                }
                                if (dvs.contains("2")) {
                                    if (d >= 10000 && d <= 50000) {
                                        b = true;
                                    }
                                }
                                if (dvs.contains("3")) {
                                    if (d >= 50000 && d <= 100000) {
                                        b = true;
                                    }
                                }
                                if (dvs.contains("4")) {
                                    if (d >= 100000) {
                                        b = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 上线时间
                String ods = req.getOnline_date();
                if (!ods.contains("0")) {
                    // 0：不限 四位数年份 五位数即为检索该年份之前 例如，2018则检索2018年数据；20110则检索2011年之前数据
                    boolean b = false;
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        if (mapSub.containsKey("online_date") && mapSub.get("online_date") != null) {
                            // 2014-09-17上线
                            String online_date = String.valueOf(mapSub.get("online_date"));
                            String d[] = online_date.split(",");
                            for (String year : d) {
                                if (year.length() == 4) {
                                    if (online_date.contains(year)) {
                                        b = true;
                                        break;
                                    }
                                } else {
                                    if (year.substring(0, 4).compareTo(online_date.substring(0, 4)) > 0) {
                                        b = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }
                // 注册地
                String oas = req.getOnline_area();
                if (!oas.contains("0")) {
                    // 0：不限 1：北京 2：上海 3：广东 4：浙江 5：江苏 6：山东
                    // 7：安徽 8：重庆 9：福建 10：甘肃 11：广西 12：贵州
                    // 13：海南 14：河北 15：河南 16：黑龙江 17：湖北 18：湖南
                    // 19：吉林 20：江西 21：辽宁 22：内蒙古 23：宁夏 24：青海
                    // 25：山西 26：陕西 27：四川 28：天津 29：新疆 30：云南
                    boolean b = false;
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        if (mapSub.containsKey("online_area") && mapSub.get("online_area") != null) {
                            String online_area = String.valueOf(mapSub.get("online_area"));
                            // 29,026.11
                            if (oas.contains("1")) {
                                if (online_area.contains("北京")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("2")) {
                                if (online_area.contains("上海")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("3")) {
                                if (online_area.contains("广东")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("4")) {
                                if (online_area.contains("浙江")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("5")) {
                                if (online_area.contains("江苏")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("6")) {
                                if (online_area.contains("山东")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("7")) {
                                if (online_area.contains("安徽")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("8")) {
                                if (online_area.contains("重庆")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("9")) {
                                if (online_area.contains("福建")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("10")) {
                                if (online_area.contains("甘肃")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("11")) {
                                if (online_area.contains("广西")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("12")) {
                                if (online_area.contains("贵州")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("13")) {
                                if (online_area.contains("海南")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("14")) {
                                if (online_area.contains("河北")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("15")) {
                                if (online_area.contains("河南")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("16")) {
                                if (online_area.contains("黑龙江")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("17")) {
                                if (online_area.contains("湖北")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("18")) {
                                if (online_area.contains("湖南")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("19")) {
                                if (online_area.contains("吉林")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("20")) {
                                if (online_area.contains("江西")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("21")) {
                                if (online_area.contains("辽宁")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("22")) {
                                if (online_area.contains("内蒙古")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("23")) {
                                if (online_area.contains("宁夏")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("24")) {
                                if (online_area.contains("青海")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("25")) {
                                if (online_area.contains("山西")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("26")) {
                                if (online_area.contains("陕西")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("27")) {
                                if (online_area.contains("四川")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("28")) {
                                if (online_area.contains("天津")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("29")) {
                                if (online_area.contains("新疆")) {
                                    b = true;
                                }
                            }
                            if (oas.contains("30")) {
                                if (online_area.contains("云南")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (!b) {
                        continue;
                    }
                }

                // 热门方案
                String hss = req.getHot_solution();
                if (!hss.contains("0")) {
                    // 0：不限 1：评级百强 2：银行存管 3：加入协会 4：ICP认证 5：之家考察 6：融资平台
                    boolean b = false;

                    if (hss.contains("1")) {
                        if (map.containsKey("grade") && map.get("grade") != null) {
                            b = true;
                        }
                    }
                    if (hss.contains("2")) {
                        if (map.containsKey("strength_qualification")
                                && map.get("strength_qualification") != null) {
                            Map<String, Object> mapSub = (Map<String, Object>) map.get("strength_qualification");
                            if (mapSub.containsKey("bank_deposit_management")
                                    && mapSub.get("bank_deposit_management") != null) {
                                String bank_deposit_management = String
                                        .valueOf(mapSub.get("bank_deposit_management"));
                                if (!bank_deposit_management.contains("-")
                                        && !bank_deposit_management.contains("无存管")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (hss.contains("3")) {
                        if (map.containsKey("strength_qualification")
                                && map.get("strength_qualification") != null) {
                            Map<String, Object> mapSub = (Map<String, Object>) map.get("strength_qualification");
                            if (mapSub.containsKey("regulatory_association")
                                    && mapSub.get("regulatory_association") != null) {
                                String regulatory_association = String
                                        .valueOf(mapSub.get("regulatory_association"));
                                if (regulatory_association != null && !regulatory_association.contains("-")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    if (hss.contains("4")) {
                        if (map.containsKey("IndustryCommerceInformation")
                                && map.get("IndustryCommerceInformation") != null) {
                            Map<String, Object> mapSub = (Map<String, Object>) map
                                    .get("IndustryCommerceInformation");
                            if (mapSub.containsKey("ICP") && mapSub.get("ICP") != null) {
                                String ICP = String.valueOf(mapSub.get("ICP"));
                                if (!ICP.contains("-")) {
                                    b = true;
                                }
                            }
                        }
                    }
                    // 5：之家考察 6：融资平台 没确定
                    if (!b) {
                        continue;
                    }
                }

                // bid_assort [Json Body string]标的类型 0：不限 1：新手标 2：理财计划
                // business_type [Json Body string]业务类型 0：不限 1：个人信贷 2：企业信贷 3：车贷 4：房贷
                // 5：供应链金融 6：融资租赁 7：票据 8：艺术品质押
                // 9：农村金融 10：消费金融

//				if (flg) {
                total++;
                if (start <= total && total <= end) {
                    if (map.containsKey("platform_info") && map.get("platform_info") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("platform_info");
                        item.setOnline_area(String.valueOf(mapSub.get("online_area")));
                        item.setOnline_date(String.valueOf(mapSub.get("online_date")));
                        item.setRefer_income(String.valueOf(mapSub.get("refer_income")));
                    }
                    if (map.containsKey("data") && map.get("data") != null) {
                        Map<String, Object> mapSub = (Map<String, Object>) map.get("data");
                        item.setRemain_balance(String.valueOf(mapSub.get("remain_balance")));
                    }
                    if (map.containsKey("_id")) {
                        item.setId(String.valueOf(map.get("_id")));
                    }
                    if (map.containsKey("name")) {
                        item.setName(String.valueOf(map.get("name")));
                    }
                    itemList.add(item);
                    i++;
                }
//				}
            }
        }
        if (i == 0) {
            platformSearchResponse.setTotal(i);
            platformSearchResponse.setTotal_page(i);
        } else {
            platformSearchResponse.setTotal(total);
            int all_page = total / len;
            if (all_page > 0) {
                int left = total % len;
                if (left > 0) {
                    all_page++;
                }
            }
            platformSearchResponse.setTotal_page(all_page);
        }
        platformSearchResponse.setLen(i);
        platformSearchResponse.setPage(page);
        platformSearchResponse.setItems(itemList);
        return platformSearchResponse;
    }

    public PlatDetailResponse getLoanPlatDetail(String name) {
        try {
            MongoCollection<Document> coll = mongoTemplate.getCollection(LOAN_PLANT_DETAIL_DATABASE_NAME);
            BasicDBObject filter = new BasicDBObject();
            filter.put("name", name);
            filter.put("create_time", dateFormat(new Date()));
            FindIterable<Document> iterable = coll.find(filter);
            MongoCursor<Document> cursor = iterable.iterator();
            Document platFormDetail = null;
            if (cursor.hasNext()) {
                platFormDetail = cursor.next();
            } else {
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1);
                Date yesterday = cal.getTime();
                filter = new BasicDBObject();
                filter.put("name", name);
                filter.put("create_time", dateFormat(yesterday));
                iterable = coll.find(filter);
                cursor = iterable.iterator();
                if (cursor.hasNext()) {
                    platFormDetail = cursor.next();
                } else {
                    return null;
                }
            }

            if (platFormDetail != null) {
                Map<String, Object> detail = (Map<String, Object>) platFormDetail.get("platform_detail");
                String gglist = (String) detail.get("gglist");
                Map<String, String> ggSummary = (Map<String, String>) detail.get("SE_summary");
                String[] leaders = gglist.split("[\",\",\"，\"]");
                for (int i = 0; i < (leaders.length / 2); i++) {
                    String leaderName = leaders[2 * i];
                    System.out.println("leaderName is " + leaderName);
                    String title = leaders[2 * i + 1];
                    System.out.println(title);
                    Set<String> set = ggSummary.keySet();
                    for (String key : set) {
                        String summary = ggSummary.get(key);
                        String ll = summary.substring(0, 10);
                        if (ll.contains(leaderName)) {
                            System.out.println(summary);
                        }
                    }

                }

                System.out.println("");
            }
        } catch (Exception e) {
            logger.error("fail to get plat data, the name is " + name, e);
            return null;
        }
        return null;
    }

    private String dateFormat(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
